#coding=utf-8
import time  
from selenium import webdriver  
from selenium.webdriver.common.action_chains import ActionChains

import math
import random
import numpy as np
import os
import xlrd
import pandas as pd


def batch_update(account_id=1153801501135191531, MCC=5331):
        
    driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\chromedriver_win32\chromedriver.exe')

    elem = driver.get('https://admin.paypal.com')
    elem = driver.find_element_by_id("id_accountNo")
    elem.send_keys(account_id)
    elem_search = driver.find_element_by_class_name("findUserSearch").click()
    elem = driver.get('https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number='+str(account_id))
    driver.execute_script("window.scrollTo(0, (document.body.scrollHeight)/3);")
    time.sleep(3)

    js = "document.getElementById('admin_mcc_toggle_status_val').style.display='none';"
    js1 = "document.getElementById('admin_mcc_dynamic_search').style.display='block';"
    js2 = "document.getElementById('admin_mcc').style.display='inline-block';"
    js3 = "document.getElementById('admin_mcc_toggle').style.display='block';"
    js4 = "document.getElementById('admin_mcc_toggle_save').style.display='inline-block';"
    driver.execute_script(js)
    driver.execute_script(js1)
    driver.execute_script(js2)
    driver.execute_script(js3)
    driver.execute_script(js4)
    time.sleep(3)
    driver.find_element_by_id("admin_mcc").send_keys(MCC)
    driver.find_element_by_id("memo_admin_mcc_toggle").send_keys('MCC PROJECT')
    driver.find_element_by_id("admin_mcc_toggle_save").click()
    time.sleep(5)
    driver.quit()


df = pd.read_excel("2.xlsx", sheet_name=0)     # add sheetname in so we can put all data in one excel
alertCodes = pd.read_excel("2.xlsx", sheet_name=2)
account_list = list()
MCC_list = list()
alertCodes_list = list()
print(df)
i = 0
while (i < len(df)) :
    account_list.append(df.iloc[i][0].tolist())
    MCC_list.append(df.iloc[i][1].tolist())
    i = i + 1
i = 0
while (i < len(alertCodes)) :
    alertCodes_list.append(alertCodes.iloc[i][0].tolist())
    i = i + 1
i = 0

print(alertCodes_list)

if((True not in [np.isnan(i) for i in MCC_list])and(True not in [np.isnan(i) for i in account_list])):
    for a in account_list:
        #b = "https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number="+str(a)+"#admin_restrictions_new"
        print("Updating {} time, on account {}".format(str(i+1),a))
        if MCC_list[i] in alertCodes_list:
            print("Account {}'s MCC code is in alert list, please check if you insist on updating this account".format(a))
        if MCC_list[i] not in alertCodes_list:
            batch_update(account_list[i],MCC_list[i])
            print("Successful updated!")
        # we can cancel this waitting too
        time.sleep(1)
        i += 1
        # if no need to stop, then active below break function.
        #if i == 20:
            #break
else:
    print("Sorry, please check your list and make sure each accounts has an MCC code")




#ActionChains(driver).move_by_offset(200, 100).click().perform()   #Click on some elements.

# change by browsers
'''
def browser(browser):
  try:
       if browser=="firefox":
        driver=webdriver.Firefox()
        return driver
       elif browser=="chrome":
           driver=webdriver.Chrome(r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
           return driver
       else:
           print("Not found browser!You can enter 'firefox','chrome'")
    except Exception as msg:
        print("open browser error:%s"%msg)


if __name__=="__main__":

  # 用谷歌浏览器打开天龟网页
    driver_chrome=browser("chrome")
    driver_chrome.get("http://www.tianguiedu.com")
    # 获取浏览器的名称
    print("open browser: %s"%driver_chrome.name)
    # 获取网页名称
    print(driver_chrome.title)
    time.sleep(5)  #休眠5秒
    driver_chrome.quit() #谷歌浏览器不关闭网页会报错

'''
